package org.small.shop.phone.api.process.orders.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.small.shop.phone.api.process.addr.mapper.TblWxAddrMapper;
import org.small.shop.phone.api.process.addr.pojo.TblWxAddr;
import org.small.shop.phone.api.process.coupon.mapper.TblWxCouponMapper;
import org.small.shop.phone.api.process.coupon.pojo.TblWxCoupon;
import org.small.shop.phone.api.process.goods.pojo.GoodsInfo;
import org.small.shop.phone.api.process.logorders.mapper.TblWxLogOrdersMapper;
import org.small.shop.phone.api.process.logorders.pojo.TblWxLogOrders;
import org.small.shop.phone.api.process.orderdiscount.mapper.TblWxOrderDiscountMapper;
import org.small.shop.phone.api.process.orderdiscount.pojo.TblWxOrderDiscount;
import org.small.shop.phone.api.process.ordergoods.mapper.TblWxOrderGoodsMapper;
import org.small.shop.phone.api.process.ordergoods.pojo.TblWxOrderGoods;
import org.small.shop.phone.api.process.orders.mapper.TblWxOrdersMapper;
import org.small.shop.phone.api.process.orders.pojo.CreateOrderVo;
import org.small.shop.phone.api.process.orders.pojo.OrderGoodsInfo;
import org.small.shop.phone.api.process.orders.pojo.OrderListVo;
import org.small.shop.phone.api.process.orders.pojo.SelectOrders;
import org.small.shop.phone.api.process.orders.pojo.TblWxOrders;
import org.small.shop.phone.api.process.orders.pojo.UnpaidOrdersVo;
import org.small.shop.phone.api.process.userrank.mapper.TblWxUserrankMapper;
import org.small.shop.phone.api.process.userrank.pojo.BuyFull;
import org.small.shop.phone.api.process.userrank.pojo.TblWxUserrank;
import org.small.shop.phone.api.process.users.mapper.TblWxUsersMapper;
import org.small.shop.phone.api.process.users.pojo.TblWxUsers;
import org.small.shop.phone.api.utils.format.FormatUtils;
import org.small.shop.phone.api.utils.idworker.Sid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class TblWxOrdersServiceImpl implements TblWxOrdersService {

	@Autowired
	private TblWxOrdersMapper tblWxOrdersMapper;

	@Autowired
	private TblWxOrderGoodsMapper tblWxOrderGoodsMapper;

	@Autowired
	private TblWxLogOrdersMapper tblWxLogOrdersMapper;

	@Autowired
	private TblWxOrderDiscountMapper tblWxOrderDiscountMapper;

	@Autowired
	private TblWxUsersMapper tblWxUsersMapper;

	@Autowired
	private TblWxUserrankMapper tblWxUserrankMapper;

	@Autowired
	private TblWxAddrMapper tblWxAddrMapper;

	@Autowired
	private TblWxCouponMapper tblWxCouponMapper;

	@Override
	public CreateOrderVo getCreateOrderInfo(String appId, CreateOrderVo orderInfo) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);

		if (users != null && !StringUtils.isEmpty(users.getUserId())) {

			// 保存用户信息
			orderInfo.setOrdSubscriberName(users.getUserName());
			orderInfo.setOrdSubscriberPhone(users.getUserPhone());

			// 获取默认配送地址
			TblWxAddr addr = new TblWxAddr();
			Example addrExample = new Example(TblWxAddr.class);
			Criteria addrCriteria = addrExample.createCriteria();
			addrCriteria.andEqualTo("userId", users.getUserId());
			addrCriteria.andEqualTo("addrIsdefault", 1);
			addrCriteria.andEqualTo("addrStatus", 1);
			addr = tblWxAddrMapper.selectOneByExample(addrExample);
			if (addr != null) {
				orderInfo.setAddrId(addr.getAddrId());
				orderInfo.setAddrUseraddress(addr.getAddrUseraddress());
				orderInfo.setAdrrUsername(addr.getAdrrUsername());
				orderInfo.setAdrrUserphone(addr.getAdrrUserphone());
			}
			return orderInfo;
		}

		throw new Exception();
	}

	@Override
	@Transactional
	public void createOrder(String appId, String orderId, String ordNum, String buyNum, Integer addrId,
			String ordSubscriberName, String ordSubscriberPhone, String ordRemarks, CreateOrderVo orderInfo)
			throws Exception {

		// 获取用用户的userId
		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {

			String userId = users.getUserId();
			TblWxUserrank tblWxUserrank = tblWxUserrankMapper.selectByPrimaryKey(users.getRankId());
			TblWxAddr addr = tblWxAddrMapper.selectByPrimaryKey(addrId);

			Long createOrderTime = System.currentTimeMillis();
			List<String> discntDescList = new ArrayList<String>();

			// 结算所有商品的总金额
			String[] buyNums = buyNum.split(",");
			BigDecimal ordGoodsmoney = getOrdGoodsmoney(orderInfo, buyNums);

			// 获取邮费
			BigDecimal freightAmt = getGoodsFreightAmt(orderInfo, addr, buyNum);

			// 获取会员打折优惠
			Double rankDiscount = getRankDiscount(tblWxUserrank);

			// 获取会员买满立减金额
			BuyFull buyFull = null;
			List<BuyFull> buyFullAmtList = getBuyFullAmt(ordGoodsmoney, tblWxUserrank, createOrderTime);
			if (buyFullAmtList != null) {
				buyFull = buyFullAmtList.get(0);
			}

			// 获取优惠券信息
			TblWxCoupon coupon = null;
			List<TblWxCoupon> couponList = getAllUsersCouponList(userId, ordGoodsmoney, createOrderTime);
			if (couponList != null && !couponList.isEmpty()) {
				coupon = couponList.get(0);
			}

			// 结算实际金额
			StringBuilder sb = new StringBuilder();
			BigDecimal ordRealtotalmoney = ordGoodsmoney;
			if (rankDiscount != null && buyFull == null) {

				ordRealtotalmoney = ordRealtotalmoney.multiply(new BigDecimal((rankDiscount / 10) + ""));
				sb.append(tblWxUserrank.getRankName()).append("（打").append(rankDiscount).append("折）");
				discntDescList.add(sb.toString());
			} else if (rankDiscount == null && buyFull != null) {

				ordRealtotalmoney = ordRealtotalmoney.subtract(buyFull.getBuyfullSubtAmt());
				sb.setLength(0);
				sb.append(tblWxUserrank.getRankName()).append("（").append(buyFull.getBuyfullTempName()).append("买满")
						.append(buyFull.getBuyfullAmt()).append(",").append("立减").append(buyFull.getBuyfullSubtAmt())
						.append("）");
				discntDescList.add(sb.toString());
			}

			if (coupon != null) {
				ordRealtotalmoney = ordRealtotalmoney.subtract(coupon.getCouponReducAmt());
				discntDescList.add(coupon.getCouponName() + "优惠券");
			}

			// 保存订单信息
			TblWxOrders order = new TblWxOrders();
			order.setOrdId(orderId);
			order.setUserId(userId);
			order.setOrdNum(ordNum);
			order.setOrdGoodsmoney(FormatUtils.getFormatBigDecimal(ordGoodsmoney, null));
			order.setOrdDelivertype(0);
			order.setOrdDelivermoney(FormatUtils.getFormatBigDecimal(freightAmt, null));
			order.setOrdTotalmoney(FormatUtils.getFormatBigDecimal(ordGoodsmoney.add(freightAmt), null));
			order.setOrdRealtotalmoney(FormatUtils.getFormatBigDecimal(ordRealtotalmoney.add(freightAmt), null));
			order.setOrdStauts(-2);
			order.setOrdPaytype(1);
			order.setOrdIspay(0);
			order.setOrdAreaid(addr.getAddrAreaid());
			order.setOrdAreaidpath(addr.getAdrrAreaidpath());
			order.setOrdUsername(addr.getAdrrUsername());
			order.setOrdUseraddress(addr.getAddrUseraddress());
			order.setOrdUserphone(addr.getAdrrUserphone());
			order.setOrdSubscriberName(ordSubscriberName);
			order.setOrdSubscriberPhone(ordSubscriberPhone);
			order.setOrdRemarks(ordRemarks);
			order.setOrdIsrefund(0);
			order.setOrdIsappraise(0);
			order.setOrdIsclosed(0);
			order.setOrdDataflag(1);
			order.setOrdCreatetime(createOrderTime);
			tblWxOrdersMapper.insertSelective(order);

			// 保存订单详情信息
			TblWxOrderGoods tblWxOrderGoods = null;
			List<GoodsInfo> goodsInfoList = orderInfo.getGoodsInfoList();
			for (int i = 0; i < goodsInfoList.size(); i++) {

				tblWxOrderGoods = new TblWxOrderGoods();
				tblWxOrderGoods.setOrdId(orderId);
				tblWxOrderGoods.setGoodsId(goodsInfoList.get(i).getGoodsId());
				tblWxOrderGoods.setSkuId(goodsInfoList.get(i).getGoodsSkuInfo().getSkuId());
				tblWxOrderGoods.setGoodsnum(Integer.parseInt(buyNums[i]));
				tblWxOrderGoods.setGoodsprice(goodsInfoList.get(i).getGoodsSkuInfo().getGoodsSkuDisct());
				tblWxOrderGoodsMapper.insertSelective(tblWxOrderGoods);
			}

			// 保存订单操作信息
			TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
			tblWxLogOrders.setLogid(Sid.nextShort());
			tblWxLogOrders.setOrdId(orderId);
			tblWxLogOrders.setLogtime(createOrderTime);
			tblWxLogOrders.setLogtype(0);
			tblWxLogOrders.setOrderstatus(-2);
			tblWxLogOrders.setUserId(userId);
			tblWxLogOrders.setLogcontent("用户下单");
			tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);

			// 保存订单优惠信息
			if (discntDescList != null && !discntDescList.isEmpty()) {
				TblWxOrderDiscount tblWxOrderDiscount = null;
				for (String discntDesc : discntDescList) {

					tblWxOrderDiscount = new TblWxOrderDiscount();
					tblWxOrderDiscount.setOrdId(orderId);
					tblWxOrderDiscount.setOrdDiscntDesc(discntDesc);
					tblWxOrderDiscountMapper.insertSelective(tblWxOrderDiscount);
				}
			}

		} else {
			throw new Exception();
		}

	}

	@Override
	public UnpaidOrdersVo getUnpaidOrderByOrdId(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {

			String userId = users.getUserId();
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ordId", ordId);
			map.put("userId", userId);

			UnpaidOrdersVo unpaidOrdersVo = tblWxOrdersMapper.getUnpaidOrderByOrdId(map);
			if (unpaidOrdersVo != null && unpaidOrdersVo.getGoodsInfoList() != null
					&& !unpaidOrdersVo.getGoodsInfoList().isEmpty()) {
				int totalNums = 0;
				for (OrderGoodsInfo orderGoodsInfo : unpaidOrdersVo.getGoodsInfoList()) {
					totalNums += orderGoodsInfo.getGoodsnum();
				}
				unpaidOrdersVo.setTotalNums(totalNums + "");
			}

			return unpaidOrdersVo;
		} else {
			throw new Exception();
		}

	}

	@Override
	public SelectOrders selectAllOrdsByUserIdOrSta(String appId, SelectOrders selectOrders) {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);

		if (users != null && !StringUtils.isEmpty(users.getUserId())) {

			String userId = users.getUserId();
			selectOrders.setUserId(userId);

			Integer currentPage = selectOrders.getCurrentPage();
			Integer perPage = selectOrders.getPerPage();
			if (currentPage != null && currentPage != -1 && perPage != null) {
				selectOrders.setStartNum((currentPage - 1) * perPage);
			} else {
				selectOrders.setStartNum(0);
				selectOrders.setPerPage(10);
			}

			Integer cuount = tblWxOrdersMapper.selectAllOrdsByUserIdOrStaCount(selectOrders);
			selectOrders.setTotalNum(cuount);

			List<OrderListVo> list = tblWxOrdersMapper.selectAllOrdsByUserIdOrSta(selectOrders);
			// -3用户拒收 -2：未付款的订单 -1：用户取消 0：待发货 1：配送中 2：用户确认收货
			// -2 ：订单为删除,-1：可退款 1：不可退款 OrdDataflag
			// 否是退款（0：否 1：是） ord_isRefund
			// 0：未完结 1：已完结 ordIsclosed
			for (OrderListVo ov : list) {

				if (ov.getOrdDataflag().intValue() == -1 && ov.getOrdIsrefund().intValue() == 1
						&& ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 0) {
					ov.setOrdStautsView("售后处理中");
				} else if (ov.getOrdDataflag().intValue() == -1 && ov.getOrdIsrefund().intValue() == 1
						&& ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 1) {
					ov.setOrdStautsView("退款成功");
				} else if (ov.getOrdIspay().intValue() == 0 && ov.getOrdIsclosed().intValue() == 1
						&& ov.getOrdStauts().intValue() == -1) {
					ov.setOrdStautsView("取消订单");
				} else if (ov.getOrdIspay().intValue() == 0 && ov.getOrdIsclosed().intValue() == 0
						&& ov.getOrdStauts().intValue() == -2) {
					ov.setOrdStautsView("待付款");
				} else if (ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 0
						&& ov.getOrdStauts().intValue() == 0) {
					ov.setOrdStautsView("待发货");
				} else if (ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 0
						&& ov.getOrdStauts().intValue() == 1) {
					ov.setOrdStautsView("配送中");
				} else if (ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 0
						&& ov.getOrdStauts().intValue() == 2) {
					ov.setOrdStautsView("待收货");
				} else if (ov.getOrdIspay().intValue() == 1 && ov.getOrdIsclosed().intValue() == 1
						&& ov.getOrdDataflag().intValue() == 1 && ov.getOrdIsrefund().intValue() == 0) {
					ov.setOrdStautsView("交易成功");
				}

				List<OrderGoodsInfo> goodsInfoList = ov.getGoodsInfoList();
				int totalNums = 0;
				if (goodsInfoList != null && !goodsInfoList.isEmpty()) {
					for (OrderGoodsInfo orderGoodsInfo : goodsInfoList) {
						totalNums += orderGoodsInfo.getGoodsnum();
					}
					ov.setTotalNums(totalNums);
				}
			}
			selectOrders.setOrderList(list);
			selectOrders.setUserId(null);
			return selectOrders;
		}
		return selectOrders;
	}

	@Override
	@Transactional
	public void abolishmentOrderStatus(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {
			String userId = users.getUserId();
			TblWxOrders tblWxOrders = tblWxOrdersMapper.selectByPrimaryKey(ordId);
			if (tblWxOrders != null && userId.equals(tblWxOrders.getUserId())
					&& tblWxOrders.getOrdIsclosed().intValue() == 0 && tblWxOrders.getOrdIspay() == 0
					&& tblWxOrders.getOrdStauts() == -2) {
				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ordId", ordId);
				map.put("userId", userId);
				tblWxOrdersMapper.updateOrderStatusByUserIdAndOrderId(map);

				// 保存订单操作信息
				TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
				tblWxLogOrders.setLogid(Sid.nextShort());
				tblWxLogOrders.setOrdId(ordId);
				tblWxLogOrders.setLogtime(System.currentTimeMillis());
				tblWxLogOrders.setLogtype(0);
				tblWxLogOrders.setOrderstatus(-1);
				tblWxLogOrders.setUserId(userId);
				tblWxLogOrders.setLogcontent("取消订单");
				tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);
			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}

	}

	@Override
	@Transactional
	public void delectOrderByOrdId(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {
			String userId = users.getUserId();
			TblWxOrders tblWxOrders = tblWxOrdersMapper.selectByPrimaryKey(ordId);
			if (tblWxOrders != null && userId.equals(tblWxOrders.getUserId())
					&& tblWxOrders.getOrdIsclosed().intValue() == 1) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ordId", ordId);
				map.put("userId", userId);
				tblWxOrdersMapper.delectOrderByOrdId(map);

				// 保存订单操作信息
				TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
				tblWxLogOrders.setLogid(Sid.nextShort());
				tblWxLogOrders.setOrdId(ordId);
				tblWxLogOrders.setLogtime(System.currentTimeMillis());
				tblWxLogOrders.setLogtype(0);
				tblWxLogOrders.setOrderstatus(tblWxOrders.getOrdStauts());
				tblWxLogOrders.setUserId(userId);
				tblWxLogOrders.setLogcontent("删除订单");
				tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);
			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}
	}

	@Override
	@Transactional
	public void refundOrders(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {
			String userId = users.getUserId();

			TblWxOrders tblWxOrders = tblWxOrdersMapper.selectByPrimaryKey(ordId);

			if (tblWxOrders != null && userId.equals(tblWxOrders.getUserId())
					&& tblWxOrders.getOrdIsclosed().intValue() == 0 && tblWxOrders.getOrdIspay() == 1
					&& tblWxOrders.getOrdDataflag() == -1) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ordId", ordId);
				map.put("userId", userId);
				tblWxOrdersMapper.refundOrdersByOrdId(map);

				// 保存订单操作信息
				TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
				tblWxLogOrders.setLogid(Sid.nextShort());
				tblWxLogOrders.setOrdId(ordId);
				tblWxLogOrders.setLogtime(System.currentTimeMillis());
				tblWxLogOrders.setLogtype(0);
				tblWxLogOrders.setOrderstatus(tblWxOrders.getOrdStauts());
				tblWxLogOrders.setUserId(userId);
				tblWxLogOrders.setLogcontent("申请退款");
				tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);
			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}

	}

	@Override
	@Transactional
	public void confirmOrdersByOrdId(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {
			String userId = users.getUserId();

			TblWxOrders tblWxOrders = tblWxOrdersMapper.selectByPrimaryKey(ordId);

			if (tblWxOrders != null && userId.equals(tblWxOrders.getUserId())
					&& tblWxOrders.getOrdIsclosed().intValue() == 0 && tblWxOrders.getOrdIspay() == 1) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ordId", ordId);
				map.put("userId", userId);
				tblWxOrdersMapper.confirmOrdersByOrdId(map);

				// 保存订单操作信息
				TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
				tblWxLogOrders.setLogid(Sid.nextShort());
				tblWxLogOrders.setOrdId(ordId);
				tblWxLogOrders.setLogtime(System.currentTimeMillis());
				tblWxLogOrders.setLogtype(0);
				tblWxLogOrders.setOrderstatus(tblWxOrders.getOrdStauts());
				tblWxLogOrders.setUserId(userId);
				tblWxLogOrders.setLogcontent("确认收货");
				tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);

				// 修改用户信息
				BigDecimal userTotalamt = users.getUserTotalamt();
				Integer rankId = null;
				Integer userTatolscore = null;
				if (userTotalamt != null) {
					userTotalamt = FormatUtils.getFormatBigDecimal(userTotalamt.add(tblWxOrders.getOrdRealtotalmoney()),
							null);
				}
				List<TblWxUserrank> userrankList = tblWxUserrankMapper.selectUserranksByRankAmtNum(userTotalamt);
				if (userrankList != null) {
					rankId = userrankList.get(0).getRankId();
				}
				if (users.getUserTatolscore() != null) {
					userTatolscore = users.getUserTatolscore()
							+ tblWxOrders.getOrdRealtotalmoney().toBigInteger().intValue() * 10;
				}
				TblWxUsers user = new TblWxUsers();
				user.setUserId(userId);
				user.setUserTotalamt(userTotalamt);
				user.setRankId(rankId);
				user.setUserTatolscore(userTatolscore);
				tblWxUsersMapper.updateTblWxUsersByUserId(user);

			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}
	}

	@Override
	@Transactional
	public void payOrdersByOrdId(String ordId, String appId) throws Exception {

		TblWxUsers users = tblWxUsersMapper.getUserNameAndPhoneByAppId(appId);
		if (users != null && !StringUtils.isEmpty(users.getUserId())) {
			String userId = users.getUserId();
			TblWxOrders tblWxOrders = tblWxOrdersMapper.selectByPrimaryKey(ordId);

			if (tblWxOrders != null && userId.equals(tblWxOrders.getUserId())
					&& tblWxOrders.getOrdIsclosed().intValue() == 0 && tblWxOrders.getOrdIspay() == 0) {

				Map<String, Object> map = new HashMap<String, Object>();
				map.put("ordId", ordId);
				map.put("userId", userId);
				map.put("ordReceivetime", System.currentTimeMillis());
				map.put("ordScore", tblWxOrders.getOrdRealtotalmoney().toBigInteger().intValue() * 10);
				tblWxOrdersMapper.payOrdersByOrdId(map);

				// 保存订单操作信息
				TblWxLogOrders tblWxLogOrders = new TblWxLogOrders();
				tblWxLogOrders.setLogid(Sid.nextShort());
				tblWxLogOrders.setOrdId(ordId);
				tblWxLogOrders.setLogtime(System.currentTimeMillis());
				tblWxLogOrders.setLogtype(0);
				tblWxLogOrders.setOrderstatus(0);
				tblWxLogOrders.setUserId(userId);
				tblWxLogOrders.setLogcontent("支付订单");
				tblWxLogOrdersMapper.insertSelective(tblWxLogOrders);

			} else {
				throw new Exception();
			}
		} else {
			throw new Exception();
		}
	}

	private List<TblWxCoupon> getAllUsersCouponList(String userId, BigDecimal ordGoodsmoney, Long createOrderTime) {
		if (!StringUtils.isEmpty(userId) && ordGoodsmoney != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userId", userId);
			map.put("buyFullAmt", ordGoodsmoney);
			map.put("startTime", createOrderTime);
			map.put("endTime", createOrderTime);
			return tblWxCouponMapper.getAllUsersCouponList(map);
		}
		return null;
	}

	private List<BuyFull> getBuyFullAmt(BigDecimal ordGoodsmoney, TblWxUserrank tblWxUserrank, Long createOrderTime) {

		if (tblWxUserrank != null && tblWxUserrank.getRankIstype() == 2 && tblWxUserrank.getRankBuyfullId() != null) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("ordGoodsmoney", ordGoodsmoney);
			map.put("startTime", createOrderTime);
			map.put("endTime", createOrderTime);
			List<BuyFull> list = tblWxUserrankMapper.getBuyFullAmt(map);
			return list;
		}

		return null;
	}

	private Double getRankDiscount(TblWxUserrank tblWxUserrank) {

		return (tblWxUserrank != null && tblWxUserrank.getRankIstype() == 1 && tblWxUserrank.getRankDiscount() != null)
				? tblWxUserrank.getRankDiscount()
				: null;
	}

	private BigDecimal getGoodsFreightAmt(CreateOrderVo orderInfo, TblWxAddr addr, String buyNum) {

		// 判定是否是同城配送或者没有设置快递模板，如果是，邮费是0
		/*
		 * if ((orderInfo.getGoodsIsfreight() != null && orderInfo.getGoodsIsfreight()
		 * == 2) || orderInfo.getGoodsFreightTempid() == null) { return new
		 * BigDecimal(0); } else { try { String useraddress = addr.getAddrUseraddress();
		 * String[] arr = useraddress.split(AppConstantUtils.SPLIT_STR1); String city =
		 * arr[1];
		 * 
		 * TblWxFreights record = new TblWxFreights(); record.setFreightAreaName(city);
		 * record.setFreightTempId(orderInfo.getGoodsFreightTempid()); TblWxFreights
		 * freights = tblWxFreightsMapper.selectOne(record);
		 * 
		 * BigDecimal firstPriorityAmt = freights.getFreightFirstPriorityAmt();
		 * BigDecimal continWeightAmt = freights.getFreightContinWeightAmt();
		 * 
		 * Double weight = orderInfo.getGoodsWeight(); // (int) Math.ceil(96.1));// 97
		 * (去掉小数凑整:不管小数是多少，都进一) int totalWeight = (int) Math.ceil(weight * buyNum);
		 * 
		 * if (firstPriorityAmt != null && continWeightAmt != null && totalWeight != 0)
		 * { if (totalWeight <= 1) { return firstPriorityAmt; } return
		 * continWeightAmt.multiply(new BigDecimal((totalWeight -
		 * 1))).add(firstPriorityAmt); }
		 * 
		 * return new BigDecimal(10); } catch (Exception e) { return new BigDecimal(10);
		 * } }
		 */
		return new BigDecimal("0");
	}

	private BigDecimal getOrdGoodsmoney(CreateOrderVo orderInfo, String[] buyNums) {

		List<GoodsInfo> infoList = orderInfo.getGoodsInfoList();
		if (infoList.size() == 1) {

			BigDecimal goodsSkuDisct = infoList.get(0).getGoodsSkuInfo().getGoodsSkuDisct();
			return goodsSkuDisct.multiply(new BigDecimal(buyNums[0]));
		} else {

			BigDecimal ordGoodsmoney = new BigDecimal("0");
			for (int i = 0; i < buyNums.length; i++) {

				if (infoList.get(i).getGoodsSkuInfo() != null
						&& infoList.get(i).getGoodsSkuInfo().getGoodsSkuDisct() != null) {
					BigDecimal goodsSkuDisct = infoList.get(i).getGoodsSkuInfo().getGoodsSkuDisct();
					ordGoodsmoney.add(goodsSkuDisct.multiply(new BigDecimal(buyNums[i])));
				}
			}
			return ordGoodsmoney;
		}
	}

}
